package com.linefly.online_car;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.linefly.online_car.Custom.MyLoading;
import com.linefly.online_car.Custom.ReLogin;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import Adapter.Adapter_chexingxuanze;
import Been.Carsbeen;
import Been.been_chexingxuanze;
import Util.JsonUtil;
import Util.NavigationBarUtil;
import Util.PreferenceUtil;
import okhttp3.Call;
import okhttp3.Response;

public class che_xing_xuan_ze extends AppCompatActivity {
    String token_Str;
    ArrayList arrayList;
    String id_store_origin,id_store_terminus,time_origin,time_terminus;
    RecyclerView recyclerView;
    List<been_chexingxuanze> list;
    Adapter_chexingxuanze adapter_chexingxuanze;
    private HashMap hashMap_detail;//用户信息的表
    View nodata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_che_xing_xuan_ze);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
         //   getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        }
        if(NavigationBarUtil.hasNavigationBar(this)){
            NavigationBarUtil.initActivity(findViewById(android.R.id.content));//解决底部适配
        }
        //获取当前登录状态

        token_Str = PreferenceUtil.getString("token", "");

        nodata = (View)findViewById(R.id.nodata);




        recyclerView = (RecyclerView)findViewById(R.id.recycler);
        list =new ArrayList<>();
        arrayList = new ArrayList();
        init();




    }

    public void init(){
        MyLoading.show(this);
        try {
            String url = getString(R.string.url) + "/device/index";

            OkHttpUtils.get(url)
                    .connTimeOut(10000)
                    .params("token",token_Str)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            MyLoading.cancel();
                            Object object = null;
                            object = JsonUtil.jsonParse(s);
                            if(object instanceof HashMap) {
                                final HashMap hashMap = (HashMap) object;
                                if (hashMap.containsKey("err")) ;
                                int err = Integer.valueOf(hashMap.get("err").toString());
                                if (err == 0) {
                                    ArrayList lists = new ArrayList();
                                    arrayList = (ArrayList)hashMap.get("list");
                                    lists = (ArrayList)hashMap.get("list");
                                    for (int i = 0; i < lists.size(); i++) {
                                        HashMap hashMap1 = (HashMap) lists.get(i);
                                        String name = hashMap1.get("car_no").toString();
                                        String pic;
                                        if(hashMap1.get("url1").toString().length()>0){
                                           pic = hashMap1.get("url1").toString();
                                        }else if(hashMap1.get("url").toString().length()>0){
                                            pic = hashMap1.get("url").toString();
                                        }else {
                                            pic = "R.mipmap.car";
                                        }

                                        String shuoming = hashMap1.get("state").toString();
                                        String price = hashMap1.get("car_no").toString();

                                        been_chexingxuanze been = new been_chexingxuanze(name,pic,shuoming,price);
                                        list.add(been);

                                    }
                                    if(list.size() == 0){
                                        nodata.setVisibility(View.GONE);
                                    }

                                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(che_xing_xuan_ze.this);
                                    adapter_chexingxuanze = new Adapter_chexingxuanze(list);
                                    recyclerView.setLayoutManager(linearLayoutManager);
                                    recyclerView.setAdapter(adapter_chexingxuanze);
                                    adapter_chexingxuanze.setItemClick(new Adapter_chexingxuanze.item_click() {
                                        @Override
                                        public void itemclick(int position) {
                                            HashMap hashMap1 = (HashMap) arrayList.get(position);
                                            EventBus.getDefault().post(new Carsbeen(hashMap1.get("id").toString(),999,999.0,999.0,1,1));
                                            finish();

                                        }
                                    });


                                }else if(err < 0){
                                    ReLogin.login(che_xing_xuan_ze.this);
                                }else {
                                    Toast.makeText(che_xing_xuan_ze.this,hashMap.get("msg").toString(),Toast.LENGTH_LONG).show();
                                }
                            }


                        }

                        @Override
                        public void onError(Call call, Response response, Exception e) {
                            MyLoading.cancel();
                            Toast.makeText(che_xing_xuan_ze.this, "连接服务器失败", Toast.LENGTH_LONG).show();
                            e.printStackTrace();
                        }
                    });

        }catch (Exception e) {
            MyLoading.cancel();
            e.printStackTrace();
        }



    }

     public void click_back(View view){
        finish();
     }




}
